package com.acwing.partition19;

import java.io.*;

/**
 * @author `RKC`
 * @date 2022/4/18 22:20
 */
public class AC1892奶牛线路 {

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        int a = nextInt(), b = nextInt(), n = nextInt();
        int ans = 0x3f3f3f3f;
        for (int i = 0; i < n; i++) {
            int fee = nextInt(), cnt = nextInt(), state = 0;
            for (int j = 0; j < cnt; j++) {
                int t = nextInt();
                if (state == 0 && t == a) state = 1;
                else if (state == 1 && t == b) state = 2;
            }
            //只有ai和bi都不为-1且ai在bi前面的才记录结果
            if (state == 2) ans = Math.min(ans, fee);
        }
        if (ans == 0x3f3f3f3f) ans = -1;
        writer.println(ans);
        writer.flush();
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
